DeepWiki

03.a - GitHub-OAuth-Initiation

Relevant source files

This page documents the /api/auth/github endpoint, which initiates the GitHub OAuth and App installation flow. This endpoint is responsible for generating secure state parameters, embedding the Stripe session_id for payment correlation, storing CSRF protection cookies, and redirecting users to the GitHub App installation page.

For details on what happens after GitHub redirects back to the application, see OAuth Callback Handler. For information about the GitHub App installation process itself, see GitHub App Installation Flow.


The OAuth initiation endpoint is implemented as a GET route handler at /api/auth/github and serves as the entry point for connecting a user's GitHub account after payment completion.

Route Location and Entry PointLink copied!

AttributeValue
File Pathapp/api/auth/github/route.ts
HTTP MethodGET
Route Pattern/api/auth/github
Query Parameterssession_id (optional, passed from success page)
Return TypeHTTP 302 Redirect to GitHub

The endpoint is invoked when users click the "Connect GitHub" button on the success page app/success/page.tsx L54

Request Flow Diagram

Sources: app/api/auth/github/route.ts L1-L44

app/success/page.tsx L54-L59


The endpoint generates a state parameter that serves two purposes: CSRF protection and payment-to-installation correlation. This state encodes both a random UUID and the Stripe session_id.

The state generation logic app/api/auth/github/route.ts L16-L20

:

FieldPurposeSource
uuidCSRF protection token (random)crypto.randomUUID()
sessionIdLinks payment to installationQuery parameter from success page
EncodingURL-safe transmissionBase64 encoding of JSON string

The endpoint extracts the session_id from the query string app/api/auth/github/route.ts L12-L13

and constructs a state object containing both the CSRF token and session identifier. This state is then Base64-encoded app/api/auth/github/route.ts L20

to create a URL-safe string that GitHub will return unchanged in the callback.

Console Logging: The endpoint logs the state generation process at three stages app/api/auth/github/route.ts L22-L24

:

  1. Original session_id value
  2. Decoded stateData object structure
  3. Final encoded state string

These logs are critical for debugging payment-to-installation correlation issues in Vercel logs.

Sources: app/api/auth/github/route.ts L11-L24


The generated state parameter is stored in an HTTP-only cookie to enable server-side verification during the OAuth callback. This prevents CSRF attacks where malicious sites attempt to complete OAuth flows with attacker-controlled state values.

The cookie is configured with the following security attributes app/api/auth/github/route.ts L29-L34

:

AttributeValueSecurity Purpose
path/Cookie available to all routes
securetrue in productionPrevents transmission over HTTP
httpOnlytruePrevents JavaScript access (XSS mitigation)
maxAge3600 seconds (1 hour)Limits exposure window for stolen cookies

The cookie name github_oauth_state app/api/auth/github/route.ts L29

is referenced by the callback handler to retrieve and validate the state parameter. This name must match the retrieval logic in /api/auth/github/callback.

Sources: app/api/auth/github/route.ts L26-L34


After generating and storing the state parameter, the endpoint constructs a redirect URL to the GitHub App installation page.

The endpoint requires the GITHUB_APP_SLUG environment variable app/api/auth/github/route.ts L5

to construct the installation URL. If this variable is missing, the endpoint returns a 500 error app/api/auth/github/route.ts L7-L9

ComponentExample ValueDescription
Base URLhttps://github.com/apps/GitHub's app installation base path
App Sluggodeep-wiki-integrationYour GitHub App's unique identifier
Path/installations/newTriggers new installation flow
Query Param?state=eyJ1dWlkIjoi...Passes encoded state back to callback

The endpoint uses Next.js's redirect() function app/api/auth/github/route.ts L43

to issue an HTTP 302 redirect. This is a server-side redirect that occurs before any response is sent to the client.

When users land on the GitHub installation page, they will:

  1. See the GitHub App's permission requests
  2. Select which repositories to grant access to
  3. Complete the installation, triggering GitHub to redirect back to the callback URL configured in the GitHub App settings

Sources: app/api/auth/github/route.ts L5-L44


The OAuth initiation endpoint implements multiple security layers to protect against common web vulnerabilities.

ThreatMitigationImplementation
CSRF AttacksRandom UUID in state + cookie verificationcrypto.randomUUID() app/api/auth/github/route.ts L17
XSS Token TheftHTTP-only cookieshttpOnly: true app/api/auth/github/route.ts L32
Man-in-the-MiddleHTTPS enforcement in productionsecure: true (production) app/api/auth/github/route.ts L31
Session HijackingShort cookie lifespanmaxAge: 3600 (1 hour) app/api/auth/github/route.ts L33
Replay AttacksOne-time state parameterCallback handler validates and consumes state

The endpoint validates that GITHUB_APP_SLUG is configured before proceeding app/api/auth/github/route.ts L7-L9

preventing runtime errors during redirect construction. Missing configuration returns an explicit error message rather than failing silently.

Sources: app/api/auth/github/route.ts L7-L34


Primary File: app/api/auth/github/route.ts

Key Functions and Symbols:

SymbolTypePurpose
GETRoute HandlerMain endpoint function
GITHUB_APP_SLUGEnvironment VariableGitHub App identifier for redirect URL
github_oauth_stateCookie NameStores state for CSRF validation
crypto.randomUUID()Web Crypto APIGenerates CSRF token
redirect()Next.js FunctionIssues HTTP 302 redirect
cookies()Next.js FunctionAccesses cookie storage API

Related Files:

  • app/success/page.tsx L54 - Initiates OAuth flow by linking to this endpoint
  • app/api/auth/github/callback/route.ts - Validates state parameter set by this endpoint

Sources: app/api/auth/github/route.ts L1-L44

Refresh this wiki

Last indexed: 23 November 2025 (922b35)

On this page

Ask Devin about godeep.wiki-jb

03.a - GitHub-OAuth-Initiation | DeepWiki | godeep.wiki